<?php

namespace app\service\admin\projects\content;

use app\model\Common;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

class ArticleService
{

    /**
     * 获取分类
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getClassify(): array
    {
        return Common::getAllData(ArticleClassifyTable, [], ['id', 'name'], true);
    }

    /**
     * 获取文章列表
     * @param array $request
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author Du
     * @date 2021/4/1 18:05
     */
    public static function getList(array $request): array
    {
        $page = $request['page'] ?? 0;
        $pageSize = $request['limit'] ?? 10;
        $so = $request['so'] ?? '';
        $classId = $request['class_id'] ?? '';
        $filer = [];
        if ($classId !== '') {
            $filer[] = ['classify_id', '=', $classId];
        }
        if ($so !== '') {
            $filer[] = ['title', 'like', '%' . $so . '%'];
        }
        $field = ['id', 'title', 'classify_id', 'status', 'read_count', 'create_time', 'read_count'];
        return Common::getList(ArticleTable, $filer, $field, $page, $pageSize);
    }

    /**
     * 详情
     * @param array $request
     * @return array|false|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function info(array $request)
    {
        if (!isset($request['id']) || !$request['id']) {
            return false;
        }
        $field = ['id', 'classify_id', 'title', 'status', 'keywords', 'cover', 'content'];
        $res = Common::getData(ArticleTable, ['id' => $request['id']], $field);
        if ($res['keywords']) {
            $res['keywords'] = jsonToArray($res['keywords']);
        }
        return $res;
    }

    /**
     * 添加
     * @param array $request
     * @return int|string
     */
    public static function add(array $request)
    {
        if ($request['keywords']) {
            $request['keywords'] = arrayToJson($request['keywords']);
        }
        $request['user_id'] = session('manage_info')['id'];
        return Common::addData(ArticleTable, $request);
    }

    /**
     * 修改
     * @param array $request
     * @return false|int
     * @throws DbException
     */
    public static function update(array $request)
    {
        if (!isset($request['id']) || !$request['id']) {
            return false;
        }
        if ($request['keywords']) {
            $request['keywords'] = arrayToJson($request['keywords']);
        }
        return Common::updateData(ArticleTable, ['id' => $request['id']], $request);
    }

    /**
     * 删除
     * @param array $request
     * @return false|int|string
     * @throws DbException
     */
    public static function delete(array $request)
    {
        if (!isset($request['data']) || !$request['data']) {
            return false;
        }
        $list = [];
        foreach ($request['data'] as $v) {
            $list[] = $v['id'];
        }
        return Common::deleteData(ArticleTable, ['id' => $list], true);
    }
}
